"""
Phase 2: Baseline Regressions — Innovation/R&D
================================================
Tests whether demographic structure predicts innovation effort
and whether aging economies seek innovation through cross-border
capital allocation.

Output: output/tables/baseline_innovation.md, fdi_innovation.md
"""

import sys
from pathlib import Path
import numpy as np
import pandas as pd

PROJECT_DIR = Path("/mnt/c/demographics_capital_flows/innovation")
ROOT_DIR = PROJECT_DIR.parent
sys.path.insert(0, str(ROOT_DIR / "multilateral" / "src"))
from model import PanelGLS

PROCESSED_DIR = PROJECT_DIR / "data" / "processed"
TABLES_DIR = PROJECT_DIR / "output" / "tables"

OECD_38 = [
    "AUS", "AUT", "BEL", "CAN", "CHL", "COL", "CRI", "CZE", "DNK", "EST",
    "FIN", "FRA", "DEU", "GRC", "HUN", "ISL", "IRL", "ISR", "ITA", "JPN",
    "KOR", "LVA", "LTU", "LUX", "MEX", "NLD", "NZL", "NOR", "POL", "PRT",
    "SVK", "SVN", "ESP", "SWE", "CHE", "TUR", "GBR", "USA",
]


def stars(p):
    if p < 0.01: return '***'
    if p < 0.05: return '**'
    if p < 0.10: return '*'
    return ''


def run_model(df, dep_var, regressors, label):
    regressors = [r for r in regressors if r in df.columns]
    if dep_var not in df.columns:
        return None
    sub = df.dropna(subset=[dep_var] + regressors).copy()
    if len(sub) < 50:
        print(f"  [{label}] Insufficient obs ({len(sub)}) — skipping")
        return None
    gls = PanelGLS()
    gls.fit(sub[dep_var].values, sub[regressors].values,
            sub['iso3'].values, sub['year'].values)
    print(f"\n  [{label}]  N={gls.n_obs}, countries={gls.n_countries}, R²={gls.r_squared:.4f}")
    results = {
        'label': label, 'dep_var': dep_var,
        'n_obs': gls.n_obs, 'n_countries': gls.n_countries,
        'r_squared': gls.r_squared,
    }
    for i, name in enumerate(regressors):
        results[f'coef_{name}'] = gls.beta[i]
        results[f'se_{name}'] = gls.se[i]
        results[f'p_{name}'] = gls.pvalues[i]
        sig = stars(gls.pvalues[i])
        print(f"    {name:<30} {gls.beta[i]:>10.4f} ({gls.se[i]:.4f}) {sig}")
    return results


def build_table(results, key_vars, notes, filename, title):
    if not results:
        return
    md = [f"# {title}\n"]
    md.append("| Model | Dep Var | N | Countries | R² |")
    md.append("|---|---|---|---|---|")
    for r in results:
        md.append(f"| {r['label']} | {r['dep_var']} | {r['n_obs']:,} "
                  f"| {r['n_countries']} | {r['r_squared']:.3f} |")
    md.append("\n## Key Coefficients\n")
    md.append("| Model | Variable | Coef | SE | p-value | Sig |")
    md.append("|---|---|---|---|---|---|")
    for r in results:
        for var in key_vars:
            ckey = f'coef_{var}'
            if ckey in r:
                p = r[f'p_{var}']
                md.append(f"| {r['label']} | {var} | {r[ckey]:.4f} "
                          f"| {r[f'se_{var}']:.4f} | {p:.4f} | {stars(p)} |")
    md.append(f"\n*{notes}*")
    out = TABLES_DIR / filename
    out.write_text('\n'.join(md))
    print(f"\n  Saved: {out}")


def main():
    print("=" * 70)
    print("PHASE 2: Baseline — Innovation/R&D")
    print("=" * 70)

    df = pd.read_csv(PROCESSED_DIR / "innovation_panel.csv")
    print(f"Panel: {df['iso3'].nunique()} countries, {len(df):,} obs")

    demo_vars = ['Z_1', 'Z_2', 'Z_3']
    age_vars = ['old_dep', 'youth_dep']
    controls = ['rgdp_growth', 'kaopen']
    controls = [c for c in controls if c in df.columns]
    controls_inc = controls + (['log_gdp_pc'] if 'log_gdp_pc' in df.columns else [])

    oecd = df[df['oecd'] == 1].copy()
    non_oecd = df[df['oecd'] == 0].copy()

    # ═══════════════════════════════════════════════════════════════════
    # PART A: Z → INNOVATION EFFORT
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART A: Z → INNOVATION EFFORT")
    print("=" * 70)

    a_results = []

    for dep, label in [
        ('rd_expenditure_gdp', 'A1: Z → R&D/GDP'),
        ('log_rd', 'A2: Z → log(R&D)'),
        ('log_patents', 'A3: Z → log(patents)'),
        ('patents_per_million', 'A4: Z → patents/million'),
        ('hightech_exports_share', 'A5: Z → hightech exports %'),
        ('scientific_articles', 'A6: Z → scientific articles'),
    ]:
        r = run_model(df, dep, demo_vars + controls_inc, label)
        if r: a_results.append(r)

    # Age ratios
    r = run_model(df, 'rd_expenditure_gdp', age_vars + controls_inc,
                  "A7: age ratios → R&D")
    if r: a_results.append(r)

    # OECD vs non-OECD
    r = run_model(oecd, 'rd_expenditure_gdp', demo_vars + controls_inc,
                  "A8: OECD Z → R&D")
    if r: a_results.append(r)
    r = run_model(non_oecd, 'rd_expenditure_gdp', demo_vars + controls_inc,
                  "A9: non-OECD Z → R&D")
    if r: a_results.append(r)

    # Income terciles
    for group in ['low', 'mid', 'high']:
        sub = df[df['income_group'] == group].copy()
        r = run_model(sub, 'rd_expenditure_gdp', demo_vars + controls,
                      f"A10: {group} Z → R&D")
        if r: a_results.append(r)

    build_table(a_results, demo_vars + age_vars,
                f"Controls: {', '.join(controls_inc)}",
                "baseline_innovation.md",
                "Demographics → Innovation Effort")

    # ═══════════════════════════════════════════════════════════════════
    # PART B: Z → CROSS-BORDER INNOVATION FLOWS
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART B: Z → CROSS-BORDER INNOVATION FLOWS")
    print("=" * 70)

    b_results = []

    # Non-resident patent share (do aging countries attract foreign innovation?)
    if 'nonres_patent_share' in df.columns:
        r = run_model(df, 'nonres_patent_share', demo_vars + controls_inc,
                      "B1: Z → nonres patent share")
        if r: b_results.append(r)

    # FDI inflows (innovation-seeking?)
    if 'fdi_inflows_gdp' in df.columns:
        r = run_model(df, 'fdi_inflows_gdp', demo_vars + controls_inc,
                      "B2: Z → FDI inflows/GDP")
        if r: b_results.append(r)

    # FDI outflows (aging → export capital to innovate abroad?)
    if 'fdi_outflows_gdp' in df.columns:
        r = run_model(df, 'fdi_outflows_gdp', demo_vars + controls_inc,
                      "B3: Z → FDI outflows/GDP")
        if r: b_results.append(r)

    # Hightech exports (revealed comparative advantage)
    r = run_model(df, 'hightech_exports_share', demo_vars + controls_inc,
                  "B4: Z → hightech export share")
    if r: b_results.append(r)

    # OECD FDI outflows
    if 'fdi_outflows_gdp' in df.columns:
        r = run_model(oecd, 'fdi_outflows_gdp', demo_vars + controls_inc,
                      "B5: OECD Z → FDI outflows")
        if r: b_results.append(r)

    build_table(b_results, demo_vars,
                "Cross-border innovation flows",
                "fdi_innovation.md",
                "Demographics → Cross-Border Innovation Flows")

    # ═══════════════════════════════════════════════════════════════════
    # PART C: LAGGED & ROBUSTNESS
    # ═══════════════════════════════════════════════════════════════════
    print("\n" + "=" * 70)
    print("PART C: LAGGED & ROBUSTNESS")
    print("=" * 70)

    c_results = []

    lag_vars = ['Z_1_lag5', 'Z_2_lag5', 'Z_3_lag5']
    r = run_model(df, 'rd_expenditure_gdp', lag_vars + controls_inc,
                  "C1: Z_lag5 → R&D")
    if r: c_results.append(r)

    diff_vars = ['d_Z_1', 'd_Z_2', 'd_Z_3']
    r = run_model(df, 'rd_expenditure_gdp', diff_vars + controls_inc,
                  "C2: ΔZ → R&D")
    if r: c_results.append(r)

    build_table(c_results, lag_vars + diff_vars,
                "Lagged and first-differenced demographics",
                "robustness.md",
                "Robustness: Lagged Demographics → Innovation")

    print("\n" + "=" * 70)
    print("Phase 2 complete.")
    print("=" * 70)


if __name__ == "__main__":
    main()
